package com.zjut.an.log.action;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zjut.an.log.dao.AnLog;
import com.zjut.an.log.manager.AnLogManager;
import com.zjut.an.util.module.PageModule;
import com.zjut.an.util.tools.JSONUtilClass;
import com.zjut.an.util.tools.JSONUtilClassTime;
import com.zjut.an.util.tools.MD5;

public class AnLogAction extends ActionSupport implements
ServletResponseAware, ServletRequestAware, ModelDriven<AnLog>{

	private HttpServletRequest request;
	private HttpServletResponse response;
	private HttpSession session;
	
	private AnLog obj;
	private Map<String , Object> dataMap;
	private int page = 1, limit = 10;
	private String order;
	private String sort;
	private String search;
	private String rowstr;
	
	@Resource(name = "AnLogManager")
	private AnLogManager manager;
	
	public String anLogList() throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		dataMap = new HashMap<String, Object>();
		if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
			if(search!=null) {
				obj = new AnLog();
				obj.setLoValue(search);
			}
			PageModule module = manager.getDataByInstance(page, limit, sort, order, obj);
			dataMap.put("rows", module.getObjects());
			dataMap.put("total", module.getTotal()); 
		} else {//否则就提供考生ID进行日志输出
			String usId = request.getParameter("usId");
			String key = request.getParameter("key");
			if(MD5.toMD5(usId.toString()).equals(key)) {
				if(search!=null) {
					obj = new AnLog();
					obj.setLoValue(search);
				}
				PageModule module = manager.getDataByInstance(page, limit, sort, order, obj);
				dataMap.put("rows", module.getObjects());
				dataMap.put("total", module.getTotal()); 
			}
		}
		obj = null; 
		return SUCCESS;
	}
	
	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	@SuppressWarnings("unchecked")
	public String anLogDeletes() {
		String objumniJSON = rowstr;
		manager.deleteDatas(JSONUtilClassTime.getDTOList(objumniJSON, AnLog.class));
		dataMap = new HashMap<String, Object>();
		dataMap.put("flag", "success");
		return SUCCESS;
	}
	
	public String anLogAdd() {
		String objumniJSON = rowstr;
		obj = (AnLog)JSONUtilClass.getDTO(objumniJSON, AnLog.class);
		manager.addData(obj);
		return SUCCESS;
	}
	
	public String anLogDelete() {
		String objumniJSON = rowstr;
		manager.deleteData((AnLog)JSONUtilClassTime.getDTO(objumniJSON, AnLog.class));
		dataMap = new HashMap<String, Object>();
		dataMap.put("flag", "success");
		return SUCCESS;
	}
	
	public String anLogUpdate() {
		String objumniJSON = rowstr;
		manager.updateData((AnLog)JSONUtilClassTime.getDTO(objumniJSON, AnLog.class));
		return SUCCESS;
	}
	
	public String anLogSave() {
		obj = (AnLog)(JSONUtilClass.getDTOArray(rowstr, AnLog.class)[0]);
		if (obj.getLoId()!=null) {
			//-------update-------
			manager.updateData(obj);
		} else {
			//-------insert-------
			manager.addData(obj);
		}
		rowstr = null;
		return SUCCESS;
	}
	
	@Override
	public AnLog getModel() {
		if (null == obj) {
			obj = new AnLog();
		}
		return obj;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
		session = request.getSession();
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		response = arg0;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public AnLog getAl() {
		return obj;
	}

	public void setAl(AnLog obj) {
		this.obj = obj;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public String getRowstr() {
		return rowstr;
	}

	public void setRowstr(String rowstr) {
		this.rowstr = rowstr;
	}

	public AnLogManager getManager() {
		return manager;
	}

	public void setManager(AnLogManager manager) {
		this.manager = manager;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

}
